package net.cctms.zhishu.webAppFramework.ui.video;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;


import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import net.cctms.zhishu.R;
import net.cctms.zhishu.webAppFramework.contact.UrlName;
import net.cctms.zhishu.webAppFramework.ui.dialog.CommonDialog;
import net.cctms.zhishu.webAppFramework.ui.view.ShareDialog;
import net.cctms.zhishu.webAppFramework.utils.ShareDialogUtil;
import net.cctms.zhishu.webAppFramework.utils.UrlUtil;

/**
 * 类名 MyVideoActivity
 * 说明 视频播放界面
 */
@Route(path = "/zhishu/MyVideoActivity")
public class MyVideoActivity extends VideoActivity {

    private String from = "";
   /* *//**
     * 接口来源页面统计
     *//*
    public static final String PAGE_FROM = "from";//来源进入页面，用于接口统计

    public static final String INDEX = "index";//首页热门
    public static final String RELATIVE = "relative";//最终页相关文章

    public static final String ARTICLE_ID = "article_id";
    public static final String CONTENT_URL = "content_url";
    public static final String ORIGINAL_URL = "original_url";
    public static final String VIDEO_SUMMARY = "video_summary";*/
    @Autowired(name ="article_id")
    public String mArticleId;
    @Autowired(name ="content_url")
    public String mContentUrl;
    @Autowired(name ="original_url")
    public String mOriginalUrl;
    @Autowired(name ="video_summary")
    public String mSummary;
   /*
    @Autowired(name ="video_summary")
    public String mvideoUrl;
    @Autowired(name ="video_summary")
    public String mSummary;*/
    public CommonDialog mErrorDialog;
    /**
     * 重试次数
     */
    private int mRetryTimes = 0;

    /**
     * 打开文章视频播放页面
     *
     * @param context
     * @param articleId    文章id
     * @param videoUrl     视频链接
     * @param from         点击来源
     * @param videoTitle   视频名称
     * @param videoSummary 视频描述
     * @param contentUrl   分享链接
     * @param originalUrl  原文链接
     */
    public static void  openActivity(Context context,
                                    String articleId,
                                    String contentUrl,
                                    String originalUrl,
                                    String videoTitle,
                                    String videoUrl,
                                    String videoSummary,
                                    String from) {
        Intent intent = new Intent(context, MyVideoActivity.class);
        intent.putExtra(UrlName.VIDEO_TITLE, videoTitle);
        intent.putExtra(UrlName.VIDEO_URL, videoUrl);
        intent.putExtra(UrlName.CONTENT_URL, contentUrl);
        intent.putExtra(UrlName.ORIGINAL_URL, originalUrl);
        intent.putExtra(UrlName.VIDEO_SUMMARY, videoSummary);
        intent.putExtra(UrlName.PAGE_FROM, from);
        intent.putExtra(UrlName.ARTICLE_ID, articleId);

        if (context instanceof Activity) {
            context.startActivity(intent);
            ((Activity) context).overridePendingTransition(0, 0);
        } else {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        from = getIntent().getStringExtra(UrlName.PAGE_FROM);
        mArticleId = getIntent().getStringExtra(UrlName.ARTICLE_ID);
        mContentUrl = getIntent().getStringExtra(UrlName.CONTENT_URL);
        mOriginalUrl = getIntent().getStringExtra(UrlName.ORIGINAL_URL);
        mSummary = getIntent().getStringExtra(UrlName.VIDEO_SUMMARY);

    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
    }

    @Override
    protected void playProgress(long position, long duration) {

    }

    @Override
    protected void playerError() {
        if (mErrorDialog == null) {
            showErrorDialog();
        } else {
            if (!mErrorDialog.isShowing()) {
                showErrorDialog();
            }
        }
    }

    @Override
    protected void shareClick() {
        ShareDialogUtil.showDialog(this, new ShareDialog.OnShareItemClickListener() {
            @Override
            public void onShareItemClick(int position) {
                ShareDialogUtil.ShareEntity shareEntity = new ShareDialogUtil.ShareEntity();
                shareEntity.articleId = mArticleId;
                shareEntity.shareTitle = mVideoTitle;
                shareEntity.shareContent = mSummary;
                shareEntity.shareUrl = UrlUtil.getArticleShareUrl(mContentUrl);
                ShareDialogUtil.shareItemClickHelper(MyVideoActivity.this, position, shareEntity);
            }
        });
    }

    /**
     * 弹出错误对话框
     */
    public void showErrorDialog() {

        //这是视频播放出错的处理逻辑，

        mErrorDialog = new CommonDialog(this, getString(R.string.video_error), "", getString( R.string.video_error_jump_original_url), getString(R.string.video_error_retry), true, true, true);
        mErrorDialog.setOnDialogClickListener(new CommonDialog.OnDialogClickListener() {
            @Override
            public void onClickConfirm() {
                //WebActivity.openActivity(MyVideoActivity.this, mVideoTitle, mOriginalUrl);
                MyVideoActivity.this.finish();
            }

            @Override
            public void onClickCancel() {

                mErrorDialog.dismiss();
                //
                if (0 == mRetryTimes) {
                    //fetchArticleVideo();
                    mRetryTimes++;
                } else {
                    mVideoPlayer.release();
                    mVideoPlayer.start();
                }
            }
        });
        mErrorDialog.show();
    }



}
